package com.lsh.service.impl.advices;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsh.dao.advices.AdviceCommentDao;
import com.lsh.domain.AdviceComment;
import com.lsh.domain.UserComment;
import com.lsh.domain.UserReply;
import com.lsh.service.advices.AdviceCommentService;
import com.lsh.util.PageUtil;
import com.lsh.util.RedisUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息评价业务模块
 * @author Administrator
 * @date 2019/10/28 17:57
 */
@Service
public class AdviceCommentServiceImpl implements AdviceCommentService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RedisUtil redisUtil;

    /**
     * 获取当前用户所有未读评价记录数
     * @param userId
     * @return
     */
    @Override
    public int findUnReadCommentCount(String userId) {
        List<AdviceComment> list = redisTemplate.opsForList().range("comment_" + userId, 0, -1);
        int unCount = 0;
        for(AdviceComment ac: list)
            if(ac.getIsRead() == 0)
                ++unCount;
        return unCount;
    }

    /**
     * 获取当前用户所有收到评价的信息
     * @param userId
     * @param pageNum
     * @return
     */
    @Override
    public HashMap<String, Object> findReceiveCommentByUserId(String userId, Integer pageNum) {
        if(pageNum < 1) pageNum = 1;
        HashMap hashMap = redisUtil.startPage("comment_" + userId, pageNum, PageUtil.ADVICE_COMMENT.getPageSize());
        return hashMap;
    }

    /**
     * 更新指定index索引的用户评价的已读情况
     * @param userId
     * @return
     */
    @Override
    public void updateIsReadByUserId(String userId, Integer index) {
        AdviceComment ac = (AdviceComment) redisTemplate.opsForList().index("comment_" + userId, index);
        ac.setIsRead(1);
        redisTemplate.opsForList().set("comment_" + userId, index, ac);
    }

    /**
     * 更新用户全部评价的未读情况
     * @param userId
     */
    @Override
    public void updateIsReadAllByUserId(String userId) {
         List<AdviceComment> adviceComments = redisTemplate.opsForList().range("comment_" + userId, 0 , -1);
         for(int i=0;i<adviceComments.size();i++){
             AdviceComment ac = adviceComments.get(i);
             if(ac.getIsRead() == 0){
                 ac.setIsRead(1);
                 redisTemplate.opsForList().set("comment_" + userId, i, ac);
             }
         }
    }

    /**
     * 根据指定index索引删除当前用户评价的消息记录
     * @param userId
     * @param index
     */
    @Override
    public void deleteReceiveCommentByUserId(String userId, Integer index) {
        AdviceComment ac = (AdviceComment) redisTemplate.opsForList().index("comment_" + userId, index);
        redisTemplate.opsForList().remove("comment_" + userId, 0, ac);
    }

    /**
     * 删除当前用户所有评价的消息记录
     * @param userId
     */
    @Override
    public void deleteReceiveCommentAllByUserId(String userId) {
        redisTemplate.delete("comment_" + userId);
    }


}
